<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>住客列表</title>
    <link rel="stylesheet" href="../views/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="../views/static/admin/css/style.css">
    <script src="../views/static/common/layui/layui.js"></script>
    <script src="../views/static/common/jquery-3.3.1.min.js"></script>
    <script src="../views/static/common/vue.min.js"></script>
    <script>
        function onloadList() {
            let key = $("#searchInput").val();
            $.post("${pageContext.request.contextPath}/BooksServlet.do", {
                "action": "bookList",
                "phone": key
            }, function (data) {
                console.log("后端返回数据:", data); // 确认数据格式
                let tbody = $("#guestTableBody");
                console.log("找到的tbody数量:", tbody.length); // 必须输出1

                tbody.empty(); // 清空内容

                if (!data || data.length === 0) {
                    tbody.html('<tr><td colspan="10" style="text-align:center">没有找到数据</td></tr>');
                    return;
                }

                for (let i = 0; i < data.length; i++) {
                    let {
                        id: guestId,
                        name: guestName,
                        sex: guestSex,
                        card: guestCard,
                        phone: guestPhone,
                        enterTime: guestEnterTime,
                        exitTime: guestExitTime,
                        h_Type: guestType,
                        num: guestNum,
                        // homeId: homeId,
                        // vipId: vipId
                    } = data[i];

                    // 改用传统字符串拼接
                    let trHtml =
                        '<tr id="' + guestId + '">' +
                        '<td>' + guestId + '</td>' +
                        '<td>' + guestName + '</td>' +
                        '<td>' + guestSex + '</td>' +
                        '<td>' + guestCard + '</td>' +
                        '<td>' + guestPhone + '</td>' +
                        '<td>' + guestEnterTime + '</td>' +
                        '<td>' + guestExitTime + '</td>' +
                        '<td>' + guestType + '</td>' +
                        '<td>' + guestNum + '</td>' +
                        '<td>' +
                        '<a href="javascript:deleteBook(' + guestId + ')">删除</a>' +
                        '&nbsp;&nbsp;|&nbsp;&nbsp;' +
                        '<a href="' +
                        '${pageContext.request.contextPath}/BooksServlet.do?action=goUpdate&id=' + guestId +
                        '">修改</a>' +
                        '</td>' +
                        '</tr>';


                    tbody.append(trHtml);

                }
            }, "json")
                .fail(function (jqXHR, textStatus, errorThrown) {
                    console.error("请求失败:", textStatus, errorThrown);
                    $("#guestTableBody").html('<tr><td colspan="10" style="text-align:center">加载数据失败</td></tr>');
                });
        }

        function deleteBook(id) {
            if (confirm("是否确认删除？")) {
                $.post("${pageContext.request.contextPath}/BooksServlet.do", {
                    "action": "deleteBook",
                    "id": id
                }, function (data) {
                    if (data) {
                        alert("删除成功");
                        $("#" + id).remove();
                    } else {
                        alert("删除失败，请稍后再试");
                    }
                }, "json");
            }
        }

    </script>
</head>
<body onload="onloadList();">
<div id="app">
    <!--顶栏-->
    <jsp:include page="top.jsp" flush="true"/>

    <div class="main" id="app">
        <!--左栏-->
        <div class="left">
            <ul class="cl">
                <!--顶级分类-->
                <li v-for="vo,index in menu" :class="{hidden:vo.hidden}">
                    <a href="javascript:;" :class="{active:vo.active}" @click="onActive(index)">
                        <i class="layui-icon" v-html="vo.icon"></i>
                        <span v-text="vo.name"></span>
                        <i class="layui-icon arrow" v-show="vo.url.length==0">&#xe61a;</i>
                        <i v-show="vo.active" class="layui-icon active">&#xe623;</i>
                    </a>
                    <!--子级分类-->
                    <div v-for="vo2,index2 in vo.list">
                        <a href="javascript:;" :class="{active:vo2.active}" @click="onActive(index,index2)"
                           v-text="vo2.name"></a>
                        <i v-show="vo2.active" class="layui-icon active">&#xe623;</i>
                    </div>
                </li>
            </ul>
        </div>
        <!--右侧-->
        <div class="right">
            <div class="layui-row">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>住客列表</legend>
                </fieldset>
                <%-- 搜索 --%>
                <div class="layui-col-lg4" style="float: right">
                    <div>
                        <span style="color: red; font-weight: bold">&nbsp;${error}</span>
                    </div>
                    <div class="layui-input-inline" style="width:320px; float: left">
                        <input type="text" id="searchInput" placeholder="输入手机号进行查询" class="layui-input key">
                    </div>
                    <div style="float: right">
                        <button type="button" class="layui-btn layui-btn-normal" onclick="onloadList()"
                                style="width: 100px">查询
                        </button>
                    </div>
                </div>

            </div>

            <%-- 表格 --%>
            <table class="layui-table layui-form">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>身份证号</th>
                    <th>手机号</th>
                    <th>入住时间</th>
                    <th>退房时间</th>
                    <th>房间类型</th>
                    <th>房间号</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="guestTableBody">
                <!-- 数据将通过JavaScript动态加载 -->
                <tr>
                    <td colspan="10" style="text-align:center">加载中...</td>
                </tr>
                </tbody>
            </table>

        </div>
    </div>
</div>
<script src="../views/static/admin/js/config.js"></script>
<script src="../views/static/admin/js/script.js"></script>
</body>
</html>
